package com.chenglu.android.mine.setting.mvp.presenter;





import android.accounts.NetworkErrorException;

import com.chenglu.android.bean.LoginBean;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.base.BaseView;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.NetworkUtil;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.PutFileBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.setting.mvp.contract.SettingContract;
import com.chenglu.android.mine.setting.mvp.model.SettingModel;

import java.io.File;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.EmptyStackException;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class SettingPresenter extends BasePresenter<SettingContract.View> implements SettingContract.SettingPresenter {

     private  SettingContract.SettingModel model;

    public SettingPresenter(){
        model = new SettingModel();
    }

    @Override
    public void getUserInfo(String token) {

        if(!isViewAttached()){
            return;
        }

        model.getUserInfo(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<UserInfoBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull UserInfoBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getUserInfo(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
//                        apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    private void apiError(@NonNull Throwable e,BaseView baseView) {

        if (e instanceof ConnectException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
            ToastUtils.show("请检查您的网络状态！");
        } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
            ToastUtils.show("请求超时！");
        } else {
            //401 登录异常
            baseView.onError(e.getMessage());
        }

        LogUtils.e("qqqqqqq:" + "onError:" + e.getMessage());
    }


    @Override
    public void getUserUpdate(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }
        model.getUserUpdate(token, requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<SmsCodeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull SmsCodeBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getUserUpdate(bean);
                        }else {
                            mView.getUserUpdate(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getPutFile(String token, File file) {
        if(!isViewAttached()){
            return;
        }
        model.getPutFile(token, file)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PutFileBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull PutFileBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getPutFile(bean);
                        }else {
                            mView.getPutFile(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


}
